//  商品页面
//  CommodityViewController.m
//  GoBigMarket
//
//  Created by 李真 on 16/8/19.
//  Copyright © 2016年 legend. All rights reserved.
//

#import "CommodityViewController.h"
#import "CommodityModel.h"
#import "MyProductsTool.h"
#import "SDCycleScrollView.h"
#import "WelcomViewController.h"
static NSInteger num; //取数据库里商品数量
static NSInteger newNum;//存储num变化后的数，在网络请求成功时：num = newNum;

@interface CommodityViewController ()<SDCycleScrollViewDelegate>

@property (nonatomic,strong)UIScrollView *scrollerView;
@property (nonatomic,strong)SDCycleScrollView *cqcView;//轮播图
@property (nonatomic,strong)UILabel *nameLabel;//商品名称
@property (nonatomic,strong)UILabel *textLabel;//商品简介
@property (nonatomic,strong)UILabel *salesLabel;//销量
@property (nonatomic,strong)UILabel *speLabel;//规格,二期目标
@property (nonatomic,strong)UILabel *stockLabel;//库存
@property (nonatomic,strong)UIView *backView;//单价以及购物车底图
@property (nonatomic,strong)UILabel *numLabel;//加入购物车的数量
@property (nonatomic,strong)UIButton *addBtn;//加号按钮
@property (nonatomic,strong)UIButton *subBtn;//减号按钮
@property (nonatomic,strong)UILabel *priceLabel;//价格label;
@property (nonatomic,strong)UILabel *promotionLabel;//促销label
@property (nonatomic,strong)UILabel *totalLabel;//合计label
@property (nonatomic,strong)UIView *bottomView;//底栏
@property (nonatomic,strong)UIView *JJview;//商品名称与简介底图
@property (nonatomic,strong)UIButton *shoppingCarbtn;//购物车按钮
@property (nonatomic,strong)CommodityModel *Datamodel;

@end

@implementation CommodityViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

#pragma mark - 查询数据库数据
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self setProductId:_ProductId];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
}

#pragma mark - tabbar设置
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}
#pragma mark - 网络请求
-(void)setProductId:(NSString *)ProductId{
    if (_ProductId != ProductId) {
        _ProductId = ProductId;
    }
    [CQNetWorkTools GETWithURL:GetProductD par:@{@"ProductId":ProductId} success:^(id responseObject) {
        if ([[responseObject valueForKey:@"Err"]integerValue] != 0 ) {
            //不成功,打印错误信息
            [SVProgressHUD  showWithStatus:[responseObject objectForKey:@"ErrMsg"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }else{
            //成功
            self.Datamodel = [CommodityModel mj_objectWithKeyValues:[responseObject valueForKey:@"Product"]];
            self.navigationItem.title = self.Datamodel.ProductName;
#pragma mark - 读取本地数据
            num = [MyProductsTool queryByCondition:[_ProductId integerValue]];
            if ( num > 0) {
                self.numLabel.hidden = NO;
                self.subBtn.hidden = NO;
            }
            //            NSLog(@"numCount__: %ld || %@",num,self.ProductId);
            [self alltext];
            [self creatView];
            [self fillData];
        }
    } filed:^(NSError *error) {
    }];
}

#pragma mark - SVProgressHUD
- (void)SVProgressHUDAction{
    //渐变而且禁止用户操作
    [SVProgressHUD showWithStatus:@"加载中..."];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
}

#pragma mark - 数据填充
- (void)fillData{
    
    self.nameLabel.text = self.Datamodel.ProductName;
    self.textLabel.attributedText = [self strSuoJinWithStr:self.Datamodel.ShortDescription];
    self.salesLabel.attributedText = [self strSuoJinWithStr:[NSString stringWithFormat:@"销量: %d",self.Datamodel.SaleCounts]];
    //库存等于0时隐藏加号
    if ([self.Datamodel.MeasureUnit isEqualToString:@"0"]) {
        self.addBtn.hidden = YES;
    }
    //    self.stockLabel.text = [NSString stringWithFormat:@"库存:%@",self.Datamodel.MeasureUnit];
    self.stockLabel.attributedText = [self strSuoJinWithStr:[NSString stringWithFormat:@"库存：%d%@",self.Datamodel.Sku,self.Datamodel.MeasureUnit]];
    NSLog(@"库存库存  %@ ---  %d",self.Datamodel.MeasureUnit,self.Datamodel.Sku);
    self.priceLabel.text = [NSString stringWithFormat:@"%.2f元",self.Datamodel.MinSalePrice];//商城价
}

//创建视图
- (void)creatView{
    if (num == 0) {
        _numLabel.hidden = YES;
        _subBtn.hidden = YES;
    }
    [self.view addSubview:[self scrollerView]];
    [self.view addSubview:[self bottomView]];
}

#pragma mark - scrollView 底图创建
- (UIScrollView *)scrollerView{
    if (!_scrollerView) {
        _scrollerView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, XNWindowWidth, XNWindowHeight - 124)];
    }
    _scrollerView.showsVerticalScrollIndicator = NO;
    _scrollerView.showsHorizontalScrollIndicator = NO;
    _scrollerView.contentSize = CGSizeMake(XNWindowWidth, 640);
    _scrollerView.backgroundColor = GDJColor(138, 184, 52);
    _scrollerView.bounces = NO;
    [_scrollerView addSubview:[self cqcView]];
    [_scrollerView addSubview:[self JJview]];
    [_scrollerView addSubview:[self salesLabel]];
    [_scrollerView addSubview:[self stockLabel]];
    [_scrollerView addSubview:[self backView]];
    return _scrollerView;
}

#pragma mark - 轮播图
- (SDCycleScrollView *)cqcView{
    
    if (!_cqcView) {
        _cqcView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, XNWindowWidth, XNWindowWidth / 16 * 9) delegate:self placeholderImage:nil];
    }
    //刷新轮播图
    NSMutableArray *arr = [NSMutableArray array];
    for (NSString *str in self.Datamodel.ImagePath) {
        [arr addObject:[NSString stringWithFormat:@"%@%@",ReleaseORDebug,str]];
    }
    _cqcView.showPageControl = YES;
    _cqcView.currentPageDotColor = [UIColor whiteColor];
    _cqcView.imageURLStringsGroup = arr;
    _cqcView.showPageControl = YES;
    _cqcView.backgroundColor = [UIColor whiteColor];
    return _cqcView;
}

#pragma mark - 商品介绍
- (UIView *)JJview{
    
    if (!_JJview) {
        _JJview = [[UIView alloc]initWithFrame:CGRectMake(0, _cqcView.bottom, XNWindowWidth, 180)];
    }
    _JJview.backgroundColor = [UIColor whiteColor];
    [_JJview addSubview:[self nameLabel]];
    [_JJview addSubview:[self textLabel]];
    return _JJview;
}

//名称label
- (UILabel *)nameLabel{
    
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, XNWindowWidth, 60)];
    }
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    _nameLabel.numberOfLines = 0;
    _nameLabel.textColor = [UIColor blackColor];
    _nameLabel.font = [UIFont systemFontOfSize:20];
    return _nameLabel;
}
//简介label
- (UILabel *)textLabel{
    if (!_textLabel) {
        _textLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, _nameLabel.bottom, XNWindowWidth, 160)];
    }
    
    return _textLabel;
}

#pragma mark - 销量salesLabel
- (UILabel *)salesLabel{
    if (!_salesLabel) {
        _salesLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, _JJview.bottom + 1, XNWindowWidth, 50)];
    }
    _salesLabel.backgroundColor = [UIColor whiteColor];
    
    _salesLabel.textAlignment = NSTextAlignmentLeft;
    return _salesLabel;
}

#pragma mark - 规格speLabel
- (UILabel *)speLabel{
    
    if (!_speLabel) {
        _speLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, _salesLabel.bottom + 1, XNWindowWidth, 50)];
    }
    _speLabel.backgroundColor = [UIColor whiteColor];
    _speLabel.textAlignment = NSTextAlignmentLeft;
    return _speLabel;
}

#pragma mark - 库存stockLabel
- (UILabel *)stockLabel{
    
    if (!_stockLabel) {
        _stockLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, _salesLabel.bottom + 1, XNWindowWidth, 50)];
    }
    _stockLabel.backgroundColor = [UIColor whiteColor];
    _stockLabel.textAlignment = NSTextAlignmentLeft;
    return _stockLabel;
}
#pragma mark - 售价 && 加减号
- (UIView *)backView{
    
    if (!_backView) {
        _backView = [[UIView alloc]initWithFrame:CGRectMake(0, _stockLabel.bottom + 1, XNWindowWidth, 110)];
    }
    //初始时隐藏
    if (num == 0) {
        _subBtn.hidden = YES;
        _numLabel.hidden = YES;
    }
    _backView.backgroundColor = [UIColor whiteColor];
    [_backView addSubview:self.priceLabel];
    [_backView addSubview:self.addBtn];
    [_backView addSubview:self.numLabel];
    [_backView addSubview:self.subBtn];
    if (self.isSale) {
        [_backView addSubview:self.promotionLabel];
        NSDictionary *attrDict = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle),
                                    NSFontAttributeName: [UIFont systemFontOfSize:15] };
        self.promotionLabel.text = [NSString stringWithFormat:@"%.2f元",self.Datamodel.MarketPrice];
        self.promotionLabel.attributedText = [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"%.2f元",self.Datamodel.MarketPrice] attributes:attrDict];
    }
    return _backView;
}
//价格label
- (UILabel *)priceLabel{
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 25, 140, 60)];
    }
    _priceLabel.textAlignment = NSTextAlignmentLeft;
    [_priceLabel setCenterY:_backView.height / 2];
    
    _priceLabel.textColor = GDJColor(236, 152, 18);
    _priceLabel.font = [UIFont systemFontOfSize:25];
    return _priceLabel;
}
//促销label
-(UILabel *)promotionLabel{
    //如果是促销商品，显示促销label，并把价格label放在下面
    if (!_promotionLabel) {
        _promotionLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 140, 35)];
        _priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, _promotionLabel.bottom, 140, 55)];
        _promotionLabel.textColor = [UIColor grayColor];
    }
    return _promotionLabel;
}
//加号按钮
- (UIButton *)addBtn{
    
    if (!_addBtn) {
        _addBtn = [[UIButton alloc]initWithFrame:CGRectMake(XNWindowWidth - 45, 0, 30, 30)];
    }
    [_addBtn setCenterY:_backView.height / 2];
    [_addBtn setBackgroundImage:[UIImage imageNamed:@"可选+"] forState:0];
    [_addBtn addTarget:self action:@selector(addBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    return _addBtn;
}

//数量label
- (UILabel *)numLabel{
    
    if (!_numLabel) {
        _numLabel = [[UILabel alloc]initWithFrame:CGRectMake(_addBtn.left - 30, 0, 30, 40)];
    }
    [_numLabel setCenterY:_backView.height / 2];
    _numLabel.font = [UIFont systemFontOfSize:15.0f];
    _numLabel.text = [NSString stringWithFormat:@"%ld",num];
    _numLabel.font = [UIFont systemFontOfSize:18.0f];
    _numLabel.textAlignment = NSTextAlignmentCenter;
    return _numLabel;
}

//减号按钮
- (UIButton *)subBtn{
    
    if (!_subBtn) {
        _subBtn = [[UIButton alloc]initWithFrame:CGRectMake(_numLabel.left - 30, 0, 30, 30)];
    }
    [_subBtn setCenterY:_backView.height / 2];
    [_subBtn setBackgroundImage:[UIImage imageNamed:@"可选-"] forState:0];
    
    [_subBtn addTarget:self action:@selector(subBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    return _subBtn;
}

#pragma mark -  加号按钮触发
- (void)addBtnAction:(UIButton *)btn{
    /**判断是否登录
     */
    NSUserDefaults * userLogon = [NSUserDefaults standardUserDefaults];
    NSString * isSignin_String = [userLogon objectForKey:LOGIN_STATUS];
    NSLog(@"等没等。。。%@",isSignin_String);
    if ([isSignin_String isEqualToString:@"未登录"]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您还没有登录,请先登录后再执行操作!" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"前去登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [alert dismissViewControllerAnimated:YES completion:nil];
            WelcomViewController *welcomeVC = [[WelcomViewController alloc]init];
            UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:welcomeVC];
            [UIApplication sharedApplication].keyWindow.rootViewController = nav;
        }]];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [alert dismissViewControllerAnimated:YES completion:nil];
        }]];
        [self presentViewController:alert animated:YES completion:nil];
        
    }else if([isSignin_String isEqualToString:@"已登录"]){
        newNum = num + 1;
        if (num == 0) {
            [self addShoppingCar];
        }else{
            [self changNum];
        }
    }
    
}
#pragma mark -  减号按钮触发
- (void)subBtnAction:(UIButton *)btn{
    /**判断是否登录
     */
    NSUserDefaults * userLogon = [NSUserDefaults standardUserDefaults];
    NSString * isSignin_String = [userLogon objectForKey:LOGIN_STATUS];
    NSLog(@"等没等。。。%@",isSignin_String);
    if ([isSignin_String isEqualToString:@"未登录"]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您还没有登录,请先登录后再执行操作!" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"前去登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [alert dismissViewControllerAnimated:YES completion:nil];
            WelcomViewController *welcomeVC = [[WelcomViewController alloc]init];
            UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:welcomeVC];
            [UIApplication sharedApplication].keyWindow.rootViewController = nav;
        }]];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [alert dismissViewControllerAnimated:YES completion:nil];
        }]];
        [self presentViewController:alert animated:YES completion:nil];
        
    }else if([isSignin_String isEqualToString:@"已登录"]){
        if (num > 0 ) {
            newNum = num - 1;
            if (newNum == 0) {
                //删除商品
                [self deleteNum];
            }else{
                [self changNum];
            }
        }
    }
}

#pragma mark - 总价联动
- (void)alltext{
    float all = num * self.Datamodel.MinSalePrice;
    if (all > 0) {
        _totalLabel.attributedText = [self strColorAndSizeWithStr:[NSString stringWithFormat:@"总计:%.2f元",all]];
    }else{
        _totalLabel.text = @"总计:0.00元";
    }
}
#pragma mark - 修改购物车
- (void)changNum{
    NSDictionary *par = @{@"ShouJiHao":GETPHONE,@"Token":GETTOKEN,@"Duuid":GETUUID,@"ProductID":[NSString stringWithFormat:@"%d",self.Datamodel.Id],@"Count":[NSString stringWithFormat:@"%ld",newNum]} ;
    [CQNetWorkTools POSTWithURL:PostOperationShoppingCar par: par success:^(id responseObject) {
        DLog(@"修改购物车 ：%@",responseObject);
        NSInteger err =[[responseObject valueForKey:@"Err"]integerValue];
        
        if (err == 0) {
            num = newNum;
#pragma mark - 保存修改数据
            [MyProductsTool ClickJiaJianProduct:self.Datamodel.Id andCount:num blockcompletion:^(BOOL updata) {
                if (updata) {
                    [self alltext];
                    //修改numLabel显示，
                    _numLabel.text = [NSString stringWithFormat:@"%ld",num];
                }
            }];
#pragma mark - 查询本地数据库
            DLog(@"数据库—--: %d || %d",[MyProductsTool queryByCondition:self.Datamodel.Id],self.Datamodel.Id);
            //            [self creatRedDian];
            //            [self getCart];
        }else {
            [SVProgressHUD  showWithStatus:[responseObject objectForKey:@"msg"]];
            NSLog(@"提示提示 ：%@",responseObject[@"msg"]);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
            });
            num = 0;
            _totalLabel.text = @"总计:0.00元";
            self.subBtn.hidden = YES;
            self.numLabel.hidden = YES;
        }
    } filed:^(NSError *error) {
        DLog(@"修改购物车 error ：%@",error);
    }];
}

#pragma mark - 加入购物车
- (void)addShoppingCar{
    //加入购物车
    [CQNetWorkTools POSTWithURL:PostAddshoppingCar par:@{@"ShouJiHao":GETPHONE,@"Token":GETTOKEN,@"Duuid":GETUUID,@"ProductID":[NSString stringWithFormat:@"%d",self.Datamodel.Id],@"Count":[NSString stringWithFormat:@"%ld",newNum]} success:^(id responseObject) {
        //        NSLog(@"加入购物车 ：%@",responseObject);
        NSInteger err =[[responseObject valueForKey:@"Err"]integerValue];
        
        if (err == 0) {
            num = newNum;
#pragma mark - 添加购物数据
            [MyProductsTool ClickJiaJianProduct:self.Datamodel.Id andCount:num blockcompletion:^(BOOL updata) {
                if (updata) {
                    [self alltext];
                    //修改numLabel显示，
                    _numLabel.text = [NSString stringWithFormat:@"%ld",num];
                }
            }];
            //#pragma mark - 查询本地数据库
            DLog(@"数据库—--: %d || %d",[MyProductsTool queryByCondition:self.Datamodel.Id],self.Datamodel.Id);
            
            self.subBtn.hidden = NO;
            self.numLabel.hidden = NO;
            
//                        [self creatRedDian];
            //            [self getCart];
            
        }else {
            [SVProgressHUD  showWithStatus:[responseObject objectForKey:@"ErrMsg"]];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
            });
            num = 0;
            self.subBtn.hidden = YES;
            self.numLabel.hidden = YES;
        }
    } filed:^(NSError *error) {
        NSLog(@"发生错误：%@",error);
    }];
}
#pragma mark - 删除商品
- (void)deleteNum{
    [CQNetWorkTools POSTWithURL:PostRemoveFromCart par:@{@"ShouJiHao":GETPHONE,@"Token":GETTOKEN,@"Duuid":GETUUID,@"ProductID":[NSString stringWithFormat:@"%d",self.Datamodel.Id]}  success:^(id responseObject) {
        if ([[responseObject valueForKey:@"Err"]intValue] == 0) {
#pragma mark - 删除商品
            [MyProductsTool deleteOneProductsFromSQL:self.Datamodel.Id];
            DLog(@"数据库—--: %d || %d",[MyProductsTool queryByCondition:self.Datamodel.Id],self.Datamodel.Id);
            num = newNum;
            self.subBtn.hidden = YES;
            self.numLabel.hidden = YES;
            
            //            [self creatRedDian];
            //触发联动
            [self alltext];
        }else{
            [SVProgressHUD  showWithStatus:[responseObject objectForKey:@"ErrMsg"]];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
            });        }
        DLog(@"删除购物车里的商品: %@",responseObject);
    } filed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark - 底栏、加入购物车等等
- (UIView *)bottomView{
    if (!_bottomView) {
        _bottomView = [[UIView alloc]initWithFrame:CGRectMake(-4, XNWindowHeight - 116, XNWindowWidth + 8, 60)];
    }
    _bottomView.backgroundColor = [UIColor whiteColor];
    _bottomView.layer.borderColor = [GDJColor(237, 237, 237) CGColor];
    _bottomView.layer.borderWidth = 4;
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(self.shoppingCarbtn.right, 0, 1, _bottomView.height)];
    view.backgroundColor = GDJBackColorGray;
    
    //购物车按钮
    [_bottomView addSubview:self.shoppingCarbtn];
    
    //结算按钮
    [_bottomView addSubview:[self totalBtn]];
    //总计label
    [_bottomView addSubview:[self totalLabel]];
    [_bottomView addSubview:view];
    return _bottomView;
}

-(UIButton *)shoppingCarbtn{
    
    if (!_shoppingCarbtn) {
        _shoppingCarbtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 4, 50, 50)];
    }
    [_shoppingCarbtn addTarget:self action:@selector(shoppingCarBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_shoppingCarbtn setImage:[UIImage imageNamed:@"Shopping-Cart"] forState:0];
    _shoppingCarbtn.tintColor = [UIColor greenColor];
    [_shoppingCarbtn setTitleColor:[UIColor redColor] forState:0];
    
    return _shoppingCarbtn;
}
#pragma mark - 购物车按钮触发
- (void)shoppingCarBtnAction:(UIButton *)btn{
    if (num > 0) {
        self.tabBarController.selectedIndex = 2;
        [self.navigationController popToRootViewControllerAnimated:NO];
    }else{
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请添加要购买的商品数量" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertController addAction:cancelAction];
        [self presentViewController:alertController animated:YES completion:^{
            
        }];
    }
}

//结算按钮
- (UIButton *)totalBtn{
    UIButton *totalBtn = [[UIButton alloc]initWithFrame:CGRectMake(XNWindowWidth - 100, 0, 102, 50)];
    [totalBtn addTarget:self action:@selector(billingBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    //    totalBtn.layer.masksToBounds = YES;
    //    totalBtn.layer.cornerRadius = 10;
    //    totalBtn.layer.borderWidth = 1.3f;
    //    totalBtn.layer.borderColor = [[UIColor colorWithRed:(236)/255.0 green:(152)/255.0 blue:(18)/255.0 alpha:1.0]CGColor];
    [totalBtn setTitleColor:[UIColor whiteColor] forState:0];
    [totalBtn setBackgroundColor:GDJColor(236, 152, 18)];
    [totalBtn setTitle:@"去结算" forState:0];
    totalBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
    return totalBtn;
}
#pragma mark - 去结算按钮触发
- (void)billingBtnAction:(UIButton *)btn{
    //    NSLog(@"点击结算按钮");
    
    if (num > 0) {
        self.tabBarController.selectedIndex = 2;
        [self.navigationController popToRootViewControllerAnimated:NO];
    }else{
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请添加要购买的商品数量" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertController addAction:cancelAction];
        [self presentViewController:alertController animated:YES completion:^{
            
        }];
    }
}

-(UILabel *)totalLabel{
    
    if (!_totalLabel) {
        _totalLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 8, 140, 44)];
    }
    _totalLabel.textAlignment = NSTextAlignmentCenter;
    _totalLabel.textColor = [UIColor grayColor];
    [self alltext];
    return _totalLabel;
}

#pragma mark - 计算高度
-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    //-50减得是规格label的高度
    self.scrollerView.contentSize = CGSizeMake(XNWindowWidth, _backView.bottom);
}
#pragma mark - 富文本属性设置

//行缩进
- (NSAttributedString *)strSuoJinWithStr:(NSString *)str{
    //售价
    
    NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];
    paraStyle01.alignment = NSTextAlignmentLeft;  //对齐
    paraStyle01.headIndent = 0.0f;//行首缩进
    //参数：（字体大小17号字乘以2，34f即首行空出两个字符）
    CGFloat emptylen = 10.0f;
    paraStyle01.firstLineHeadIndent = emptylen;//首行缩进
    paraStyle01.tailIndent = 10.0f;//行尾缩进
    paraStyle01.lineSpacing = 2.0f;//行间距
    
    NSAttributedString *attrText = [[NSAttributedString alloc] initWithString:str attributes:@{NSParagraphStyleAttributeName:paraStyle01}];
    
    return attrText;
}
//字体横线
- (NSAttributedString *)strLineWithStr:(NSString *)str{
    //售价
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
    
    return attributedString;
}
//字体变色
- (NSAttributedString *)strColorAndSizeWithStr:(NSString *)str{
    //售价
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:GDJColor(236, 152, 18) range:NSMakeRange(3, str.length - 4)];
    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(3, str.length - 4)];
    
    return attributedString;
}
#pragma mark - 红点显示
- (void)creatRedDian{
    if (GETShopId) {
        [CQNetWorkTools POSTWithURL:ShopCartGetProducts par:@{@"ShouJiHao":GETPHONE,@"Token":GETTOKEN, @"Duuid":GETUUID,@"ShopId":GETShopId} success:^(id responseObject) {
            
            self.navigationController.tabBarController.viewControllers[2].tabBarItem.badgeValue = [NSString stringWithFormat:@"%@", [responseObject objectForKey:@"TotalCount"]];
            
            if ([[NSString stringWithFormat:@"%@", [responseObject objectForKey:@"TotalCount"]] isEqualToString:@"0"]) {
                self.navigationController.tabBarController.viewControllers[2].tabBarItem.badgeValue = nil;
            }
        } filed:^(NSError *error) {
            DLog(@"%@", error);
        }];
    }
}
#pragma mark - 普通弹窗
- (void)createAlertControllerWithMessage:(NSString *)message {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *centerAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertController addAction:centerAction];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:^{
        
    }];
}
@end
